package com.phone580.app;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;
import java.util.concurrent.TimeUnit;

import android.content.Context;

import com.phone580.app.dao.DownloadInfoDao;
import com.phone580.app.entity.DownloadInfoEntity;
import com.phone580.app.model.DownloadManagerModel;
import com.phone580.app.untilty.CommonUtility;

public class Downloader {

//	private static List<DownloadInfoEntity> downloadList = new ArrayList<DownloadInfoEntity>();
//
//	private static Map<Long, FutureTask<Object>> taskTrack = new HashMap<Long, FutureTask<Object>>();
//	
//	private static DiscardOldestPolicy policy = new DiscardOldestPolicy();
//
//	private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 2,
//			3, TimeUnit.HOURS, new ArrayBlockingQueue<Runnable>(100),
//			policy);
//	static {
//		threadPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
//			
//			@Override
//			public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//				r.run();
//			}
//		});
//	}
//	public static List<DownloadInfoEntity> getDownloadList() {
//		return downloadList;
//	}
//
//	public static void start(final Context context, DownloadInfoEntity info) {
////		downloadList.add(info);
////		if (CommonData.ONLY_WIFI && !CommonUtility.isWiFiActive(context)) {
////			return;
////		}
//		FutureTask<Object> task = new FutureTask<Object>(new DownloadWorker(
//				info, new DownloadCallback() {
////					private DownloadManagerModel model = new DownloadManagerModel(context);
//					@Override
//					public void success(DownloadInfoEntity info) {
////						model.updateCompleteSize(info.getId(), info.getCompleteSize());
////						model.updateStatus(info.getId(), DownloadInfoDao.FINISHED);
////						downloadList.remove(info);
////						taskTrack.remove(info.getId());
////						threadPool.purge();
////						System.out.println(threadPool.getActiveCount());
//					}
//
//					@Override
//					public void pause(DownloadInfoEntity info) {
////						if (info.isCancel()) {
////							model.remove(info.getId());
////							File temp = new File(info.getFilePath());
////							if (temp.exists()) {
////								temp.delete();
////							}
////						} else {
////							model.updateCompleteSize(info.getId(), info.getCompleteSize());
////						}
//					}
//				}), null);
////		taskTrack.put(info.getId(), task);
//		threadPool.execute(task);
//	}
//	
//	public static void pause(DownloadInfoEntity info) {
//		taskTrack.get(info.getId()).cancel(true);
//	}
//	
//	public static void cancel(DownloadInfoEntity info) {
//		info.setCancel(true);
//		taskTrack.get(info.getId()).cancel(true);
//		taskTrack.remove(info.getId());
//		threadPool.purge();
//	}

}
